/*
 * Created on 2006-9-28
 *
 */
package edu.pku.sei.modeler.locator;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CTabFolder;
import org.eclipse.swt.custom.CTabItem;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import edu.pku.sei.modeler.model.structure.ActivationLocatorAdapter;

/**
 * @author zhangle
 * @author xy 
 */
public class ActivationLocatorItem extends CTabItem {
	private ActivationLocatorAdapter locator;
	
	private Group axisGroup;

	private Group marginGroup;

	private Group breadthGroup;

	public ActivationLocatorItem(CTabFolder parent, int style, ActivationLocatorAdapter locator) {
		super(parent, style);
		this.locator = locator;
	}

	public void createControl() {
		final Composite container = new Composite(getParent(), SWT.NONE);
		setControl(container);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.verticalSpacing = 5;
		gridLayout.marginHeight = 5;
		gridLayout.numColumns = 3;
		container.setLayout(gridLayout);

		Label label = new Label(container, SWT.NONE);
		label.setText("Description: ");
		label.setLayoutData(new GridData());

		label = new Label(container, SWT.NONE);
		label.setText("auto-arrange the bounds as activations on lifeline");

		createAxisGroup(container);
		createMarginGroup(container);
		createBreadthGroup(container);
	}

	protected void createAxisGroup(Composite container) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 3;
		axisGroup = new Group(container, SWT.NONE);
		axisGroup.setText("Direction");
		axisGroup.setLayoutData(gridData);
		
		GridLayout layout = new GridLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 5;
		layout.numColumns = 3;
		axisGroup.setLayout(layout);
		
		Label directionLabel = new Label(axisGroup, SWT.NONE);
		directionLabel.setText("Direction of Axis:");
		
		final Combo directionCombo = new Combo(axisGroup, SWT.READ_ONLY);
		directionCombo.add("Vertical");
		directionCombo.add("Horizontal");
		directionCombo.select(locator.getAxisDirection() ? 0 : 1);

		directionCombo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL
				| GridData.GRAB_HORIZONTAL));
		directionCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				locator.setAxisDirection(
					directionCombo.getSelectionIndex() == 0 ? true : false);
			}
		});
		
		Label blankLabel = new Label(axisGroup, SWT.NONE);
		blankLabel.setText("");
		
		Label positionLabel = new Label(axisGroup, SWT.NONE);
		positionLabel.setText("Position (relative to the left/top of the parent):");
	
		final Text positionText = new Text(axisGroup, SWT.BORDER);
		if(locator == null) {
			positionText.setText("50");
		}
		else {
			positionText.setText(String.valueOf(locator.getPosition()));
		}
		positionText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL | GridData.FILL_HORIZONTAL));
		positionText.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent e) {
				if(locator == null)return;
				if(positionText.getText()==null || positionText.getText().equals(""))return;
				// commit value
				try {
					int iPos = Integer.parseInt(positionText.getText());
					locator.setPosition(iPos);
				}
				catch(NumberFormatException ex) {
					ex.printStackTrace();
				}
			}
		});
		
		final Combo wCombo = new Combo(axisGroup, SWT.READ_ONLY);
		wCombo.add("percent");
		wCombo.add("pixels");
		if(locator == null) {
			wCombo.select(0);
		}
		else {
			wCombo.select(locator.getPositionType() ? 0 : 1);
		}
		wCombo.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (locator == null)
					return;
				locator.setPositionType(wCombo.getSelectionIndex() == 0 ? true
						: false);
			}
		});
	}

	protected void createMarginGroup(Composite container) {
		GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 3;
		marginGroup = new Group(container, SWT.NONE);
		marginGroup.setText("Margins");
		marginGroup.setLayoutData(gridData);
		
		GridLayout layout = new GridLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 10;
		layout.numColumns = 3;
		marginGroup.setLayout(layout);

		Label label = new Label(marginGroup, SWT.NONE);
		label.setText("Head margin:");
		final Text head = new Text(marginGroup, SWT.BORDER);
		if(locator != null) {
			head.setText(String.valueOf(locator.getHeadMargin()));
		}
		head.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent e) {
				if(locator == null)return;
				if(head.getText()==null || head.getText().equals(""))return;
				// commit value
				try {
					int hMargin = Integer.parseInt(head.getText());
					locator.setHeadMargin(hMargin);
				}
				catch(NumberFormatException ex) {
					ex.printStackTrace();
				}
			}
		});
		head.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 
				| GridData.FILL_HORIZONTAL));
		label = new Label(marginGroup, SWT.NONE);
		label.setText("pixels");
		
		label = new Label(marginGroup, SWT.NONE);
		label.setText("Middle spacing:");
		final Text mid = new Text(marginGroup, SWT.BORDER);
		if(locator != null) {
			mid.setText(String.valueOf(locator.getMiddleSpacing()));
		}
		mid.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent e) {
				if(locator == null)return;
				if(mid.getText()==null || mid.getText().equals(""))return;
				// commit value
				try {
					int mMargin = Integer.parseInt(mid.getText());
					locator.setMiddleSpacing(mMargin);
				}
				catch(NumberFormatException ex) {
					ex.printStackTrace();
				}
			}
		});
		mid.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 
				| GridData.FILL_HORIZONTAL));
		label = new Label(marginGroup, SWT.NONE);
		label.setText("pixels");

		label = new Label(marginGroup, SWT.NONE);
		label.setText("Tail margin:");
		final Text tail = new Text(marginGroup, SWT.BORDER);
		if(locator != null) {
			tail.setText(String.valueOf(locator.getTailMargin()));
		}
		tail.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent e) {
				if(locator == null)return;
				if(tail.getText()==null || tail.getText().equals(""))return;
				// commit value
				try {
					int tMargin = Integer.parseInt(tail.getText());
					locator.setTailMargin(tMargin);
				}
				catch(NumberFormatException ex) {
					ex.printStackTrace();
				}
			}
		});
		tail.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 
				| GridData.FILL_HORIZONTAL));
		label = new Label(marginGroup, SWT.NONE);
		label.setText("pixels");
	}
	
	protected void createBreadthGroup(Composite container) {
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		data.horizontalSpan = 3;
		breadthGroup = new Group(container, SWT.NONE);
		breadthGroup.setText("breadth of bounds");
		breadthGroup.setLayoutData(data);
		
		GridLayout layout = new GridLayout();
		layout.marginHeight = 5;
		layout.marginWidth = 10;
		layout.numColumns = 3;
		breadthGroup.setLayout(layout);

		Label label = new Label(breadthGroup, SWT.NONE);
		label.setText("breadth:");
		final Text breadthText = new Text(breadthGroup, SWT.BORDER);
		breadthText.setEnabled(false);
		if(locator != null) {
			breadthText.setEnabled(locator.getBreadth()>0);
			breadthText.setText(String.valueOf(locator.getBreadth()));
		}
		breadthText.addKeyListener(new KeyAdapter() {
			public void keyReleased(final KeyEvent e) {
				if(locator == null)return;
				if(breadthText.getText()==null || breadthText.getText().equals(""))return;
				// commit value
				try {
					int iBreadth = Integer.parseInt(breadthText.getText());
					locator.setBreadth(iBreadth);
				}
				catch(NumberFormatException ex) {
					ex.printStackTrace();
				}
			}
		});
		breadthText.setLayoutData(new GridData(GridData.GRAB_HORIZONTAL 
				| GridData.FILL_HORIZONTAL));
		
		Composite tmpComposite = new Composite(breadthGroup, SWT.NONE);
		tmpComposite.setLayout(new FillLayout());
		
		Button fixedButton = new Button(tmpComposite, SWT.RADIO);
		fixedButton.setText("fixed");
		fixedButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				breadthText.setEnabled(true);
				locator.setBreadthType(true);
			}
		});		
		
		Button freeButton = new Button(tmpComposite, SWT.RADIO);
		freeButton.setText("free");
		freeButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				breadthText.setEnabled(false);
				locator.setBreadthType(false);
			}
		});		
		
	
		if( locator.getBreadthType() ) {
			fixedButton.setSelection(true);
		} else {
			freeButton.setSelection(true);
		}		
	}
}
